package edu.fdzc.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.fdzc.system.common.Constants;
import edu.fdzc.system.common.enums.ResultCodeEnum;
import edu.fdzc.system.common.enums.RoleEnum;
import edu.fdzc.system.entity.Accout;
import edu.fdzc.system.entity.Student;
import edu.fdzc.system.exception.CustomException;
import edu.fdzc.system.mapper.StudentMapper;
import edu.fdzc.system.service.StudentService;
import edu.fdzc.system.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student selectById(Integer integer) {
        return studentMapper.selectById(integer);
    }

    @Override
    public Accout login(Accout account) {
        Student dbStudent = studentMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbStudent)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!dbStudent.getPassword().equals(account.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        if(!"审核通过".equals(dbStudent.getStatus())){
            throw new CustomException("-1","您的注册信息尚未审核通过，暂不允许登录，请耐心等待管理员审核");
        }
        // 生成token
        String token = TokenUtils.createToken(dbStudent.getId() + "-" + dbStudent.getRole(), dbStudent.getPassword());
        dbStudent.setToken(token);
        return dbStudent;
    }

    @Override
    public void register(Accout account) {
        Student dbStudent = studentMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbStudent)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbStudent.getPassword())) {
            throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
        }
        dbStudent.setPassword(account.getNewPassword());
        studentMapper.updateById(dbStudent);
    }

    @Override
    public void updatePassword(Accout account) {
        Student student = new Student();
        BeanUtils.copyProperties(account, student);
        add(student);
    }

    @Override
    public void add(Student student) {
        Student dbStudent = studentMapper.selectByUsername(student.getUsername());
        if (ObjectUtil.isNotNull(dbStudent)) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if (ObjectUtil.isEmpty(student.getPassword())) {
            student.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(student.getName())) {
            student.setName(student.getUsername());
        }
        student.setRole(RoleEnum.STUDENT.name());
        student.setStatus("待审核");
        studentMapper.insert(student);
    }

    @Override
    public void updateById(Student student) {
        studentMapper.updateById(student);
    }

    @Override
    public void deleteById(Integer id) {
        studentMapper.deleteById(id);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        for(Integer id:ids){
            studentMapper.deleteById(id);
        }
    }

    @Override
    public List<Student> selectAll(Student student) {
        return studentMapper.selectAll(student);
    }

    @Override
    public PageInfo<Student> selectPage(Student student, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> students = studentMapper.selectAll(student);
        return PageInfo.of(students);
    }
}
